package com.woniuxy.classteacher.cofiguration;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: 牟强
 * @Date: 2020/7/18/018 14:22
 * @Description: 创建消息队列、交换机、绑定交换机与消息队列
 */
@Configuration
public class RabbitConfiguration {
    /*
        订阅分发模式
     */
    @Bean
    public Queue emailQueue() {
        return new Queue("email");
    }

    @Bean
    public Queue phoneQueue() {
        return new Queue("phone");
    }

    //交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    //绑定交换机和消息队列，目的是当交换机得到消息之后，将消息转发给对应的消息队列
    @Bean
    public Binding bindingFauntExchangeAndQueueA(FanoutExchange fanoutExchange, Queue emailQueue) {
        //将消息队列A绑定到交换机上
        return BindingBuilder.bind(emailQueue).to(fanoutExchange);
    }

    @Bean
    public Binding bindingFauntExchangeAndQueueB(FanoutExchange fanoutExchange, Queue phoneQueue) {
        //将消息队列B绑定到交换机上
        return BindingBuilder.bind(phoneQueue).to(fanoutExchange);
    }

    /**
     * 路由模式
     */
    @Bean
    public Queue routingQueueA() {
        return new Queue("routing_a");
    }

    @Bean
    public Queue routingQueueB() {
        return new Queue("routing_b");
    }

    @Bean
    public Queue routingQueueC() {
        return new Queue("routing_c");
    }

    @Bean
    public Queue routingQueueD() {
        return new Queue("routing_d");
    }

    @Bean
    public Queue routingQueueE() {
        return new Queue("routing_e");
    }

    @Bean
    public Queue routingQueueF() {
        return new Queue("routing_f");
    }


    //路由模式的交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }

    //绑定交换机、消息队列
    @Bean
    public Binding bindingDirectExchangeAndQueueA(DirectExchange directExchange, Queue routingQueueA) {
        return BindingBuilder.bind(routingQueueA).to(directExchange).with("高一");

    }

    @Bean
    public Binding bindingDirectExchangeAndQueueB(DirectExchange directExchange, Queue routingQueueB) {
        return BindingBuilder.bind(routingQueueB).to(directExchange).with("高二");

    }

    @Bean
    public Binding bindingDirectExchangeAndQueueC(DirectExchange directExchange, Queue routingQueueC) {
        return BindingBuilder.bind(routingQueueC).to(directExchange).with("高三");

    }

    @Bean
    public Binding bindingDirectExchangeAndQueueD(DirectExchange directExchange, Queue routingQueueD) {
        return BindingBuilder.bind(routingQueueD).to(directExchange).with("高一一班");

    }

    @Bean
    public Binding bindingDirectExchangeAndQueueE(DirectExchange directExchange, Queue routingQueueE) {
        return BindingBuilder.bind(routingQueueE).to(directExchange).with("高二一班");

    }

    @Bean
    public Binding bindingDirectExchangeAndQueueF(DirectExchange directExchange, Queue routingQueueF) {
        return BindingBuilder.bind(routingQueueF).to(directExchange).with("高三一班");

    }

    /*
        simple
     */
    @Bean
    public Queue codeQueue() {
        return new Queue("code");
    }
}
